package com.chunfeng.mypen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chunfeng.mypen.dao.ExamDao;
import com.chunfeng.mypen.dao.TopicExamDao;
import com.chunfeng.mypen.entity.Exam;
import com.chunfeng.mypen.entity.TopicExam;
import com.chunfeng.mypen.result.JsonRequest;
import com.chunfeng.mypen.result.exenum.RequestException;
import com.chunfeng.mypen.service.ExamService;
import com.chunfeng.mypen.vo.ExamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Description: 试卷方法实现类
 * @author: 小雷
 * @createTime: 2023-11-14 00:20:09
 */
@Service
public class ExamServiceImpl extends ServiceImpl<ExamDao, Exam> implements ExamService {

    /**
     * 试卷业务层注入
     */
    @Autowired
    private ExamDao examDao;
    @Autowired
    private TopicExamDao topicExamDao;

    /**
     * 查看所有试卷
     *
     * @param exam 试卷
     * @return JSON
     */
    @Override
    public JsonRequest<List<ExamVo>> lookExam(Exam exam) {
        List<ExamVo> examList = examDao.lookExam(exam);
        if (examList.isEmpty()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        return JsonRequest.success(examList);
    }

    /**
     * 添加试卷信息
     *
     * @param exam 待添加的试卷
     * @return JSON
     */
    @Override
    public JsonRequest<Integer> addExam(Exam exam) {
        //判断该学科的试卷名是否已经存在
        int count = this.baseMapper.selectCount(new LambdaQueryWrapper<Exam>()
                .eq(!exam.getSubjectId().isEmpty(), Exam::getSubjectId, exam.getSubjectId())
                .eq(!exam.getName().isEmpty(), Exam::getName, exam.getName()));
        if (count != 0) {
            return JsonRequest.error(RequestException.INSERT_ERROR);
        }
        int insert = this.baseMapper.insert(exam);
        if (insert < 1) {
            return JsonRequest.error(RequestException.INSERT_ERROR);
        }
        return JsonRequest.success(insert);
    }

    /**
     * 修改试卷信息
     *
     * @param exam 待修改的试卷信息
     * @return JSON
     */
    @Override
    public JsonRequest<Integer> updateExam(Exam exam) {
        //判断该学科的试卷名是否已经存在
        int count = this.baseMapper.selectCount(new LambdaQueryWrapper<Exam>()
                .eq(!exam.getId().isEmpty(), Exam::getId, exam.getId())
                .eq(!exam.getSubjectId().isEmpty(), Exam::getSubjectId, exam.getSubjectId())
                .eq(!exam.getName().isEmpty(), Exam::getName, exam.getName()));
        if (count != 0) {
            return JsonRequest.error(RequestException.UPDATE_ERROR);
        }
        int update = this.baseMapper.updateById(exam);
        if (update < 1) {
            return JsonRequest.error(RequestException.UPDATE_ERROR);
        }
        return JsonRequest.success(update);
    }

    /**
     * 删除试卷
     *
     * @param ids 试卷ID
     * @return JSON
     */
    @Override
    public JsonRequest<Integer> deleteExam(List<String> ids) {
        //判断接收的id是否为空
        if (CollectionUtils.isEmpty(ids) || ids.size() <= 0) {
            return JsonRequest.error(RequestException.DELETE_ERROR);
        }
        //判断删除的ids是否存在
        int count = examDao.selectCountByIds(ids);
        if (count != ids.size()) {
            return JsonRequest.error(RequestException.DELETE_ERROR);
        }
        //删除试卷和题目关联的数据
        topicExamDao.delete(new LambdaQueryWrapper<TopicExam>().in(TopicExam::getExamID, ids));
        //删除试卷
        int delete = this.baseMapper.deleteBatchIds(ids);
        if (delete != ids.size()) {
            return JsonRequest.error(RequestException.DELETE_ERROR);
        }
        return JsonRequest.success(delete);
    }
}
